package zhoufu.sz.sanxiao_check.ui.check;

import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ActivityPrintBinding;
import zhoufu.sz.sanxiao_check.inject.components.DaggerLoginComponet;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.CheckItemModel;
import zhoufu.sz.sanxiao_check.presents.PrintPresenter;
import zhoufu.sz.sanxiao_check.ui.BaseActivity;
import zhoufu.sz.sanxiao_check.ui.contract.BasicMsgItem2Contract;
import zhoufu.sz.sanxiao_check.utils.TextUtils;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/8/5
 * 文书打印
 */
public class PrintActivity extends BaseActivity
        implements BasicMsgItem2Contract.View<CheckItemModel>
{

    ActivityPrintBinding _binding;

    @Inject
    PrintPresenter _presenter;

    private CheckItemModel _model;

    private MenuItem item;

    @Override protected void initInject()
    {
        setTitle(getString(R.string.title_print));
        LoginComponet componet = DaggerLoginComponet.builder().activityModule(getActivityModule())
                .appComponent(getAppComponent()).build();
        componet.inject(this);
        _presenter.bindView(this);
    }

    @Override protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        _model = (CheckItemModel) getIntent().getSerializableExtra("MODEL");
        if ( _model != null )
        {
            _binding.setModel(_model);
        }
        _presenter.onStart();
    }

    @Override protected void initAfterOnCreate(ViewDataBinding binding)
    {
        _binding = (ActivityPrintBinding) binding;
    }

    @Nullable @Override protected android.databinding.DataBindingComponent setBindingComponent()
    {
        return LoginComponet.Instance.get();
    }

    @Override protected int setIndividualId()
    {
        return R.layout.activity_print;
    }

    @Override protected void onStart()
    {
        super.onStart();
    }

    @Override public String SID()
    {
        if ( _model == null )
        {

            return getIntent().getStringExtra("SID");
        } else
        {
            return _model.getSID() + "";
        }
    }

    @Override public void setModel(CheckItemModel baseModel)
    {
        if ( _model == null )
        {
            _binding.setModel(baseModel);
        } else
        {
            _model.setName(baseModel.getName());
            _model.setAddress(baseModel.getAddress());
            _model.setPlace_No(baseModel.getPlace_No());
            _model.setSID(baseModel.getSID());
        }
        item.setTitle(R.string.text_print);
    }

    @Override public void updateSuccese()
    {

    }

    @Override public void showToast(String msg)
    {
        UIUtils.showSnackbar(_binding.getRoot(), msg);
    }

    @Override public void pullFaild()
    {
        item.setTitle(R.string.text_refresh);
        UIUtils.showSnackbar(_binding.getRoot(), "获取文书数据失败");
    }

    @Override public void pullEnd()
    {
        hideProgreesDialog();
    }

    @Override public void pullStart()
    {
        showProgreesDialog();
    }

    @Override public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_print, menu);

        item = menu.findItem(R.id.menu_print);
        return super.onCreateOptionsMenu(menu);
    }

    @Override public boolean onOptionsItemSelected(MenuItem item)
    {
        if ( item.getItemId() == R.id.menu_print )
        {
            //打印
            if ( UIUtils.install(this) )
            {
                if ( _binding.getModel() == null || TextUtils
                        .isEmpty(_binding.getModel().getName()) )//获取文书信息失败
                {
                    _presenter.onStart();
                    return false;
                }
                if ( TextUtils.isEmpty(_binding.getModel().getSID() + "") )
                {
                    item.setVisible(false);
                    UIUtils.showSnackbar(_binding.getRoot(), "尚未进行隐患检查！");
                    return false;
                }
                PreHtmlViewActivity.startAct(this, _binding.getModel());
            } else {
                UIUtils.showToast(this, "请安装打印插件");
            }
        }
        return super.onOptionsItemSelected(item);
    }
}
